package com.mis.pos.service.dsl;

import com.mis.pos.dto.AddItemDTO;
import com.mis.pos.dto.ItemDTO;
import com.mis.pos.dto.ItemNumberDTO;
import com.mis.pos.entity.QItemEntity;
import com.mis.pos.entity.QItemTypeEntity;
import com.mis.pos.entity.QTypeEntity;
import com.mis.pos.form.ItemSearchForm;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: dsl版本的item查询服务类
 * @author: yuxiang
 * @create: 2019-12-10 11:26
 **/
@Service
public class ItemSearchService {

    @Autowired
    @PersistenceContext
    private EntityManager entityManager;

    //查询工厂实体
    private JPAQueryFactory queryFactory;

    //实例化控制器完成后执行该方法实例化JPAQueryFactory
    @PostConstruct
    public void initFactory(){
        queryFactory =  new JPAQueryFactory(entityManager);
    }
    //查询实体
    QItemEntity QItem = QItemEntity.itemEntity;
    QTypeEntity QType = QTypeEntity.typeEntity;
    QItemTypeEntity QItemType = QItemTypeEntity.itemTypeEntity;


    /**
     * 商品查询中的获取商品列表
     * @param searchForm
     * @return
     */
    public Map searchByItemForm(ItemSearchForm searchForm, Integer pageNumber, Integer pageSize){

        //基本谓语查询条件
        Predicate predicate = QItem.createTime.loe(new Date());
        //时间
        if(searchForm.getCreateTime()!=null){
            predicate = ExpressionUtils.and(predicate,QItem.createTime.goe(searchForm.getCreateTime()));
        }
        //品牌
        if (StringUtils.isNotBlank(searchForm.getBrand())&&StringUtils.isNotEmpty(searchForm.getBrand())){
            predicate = ExpressionUtils.and(predicate,QItem.brand.eq(searchForm.getBrand()));
        }
        //编号
        if (StringUtils.isNotBlank(searchForm.getItemNumber())&&StringUtils.isNotEmpty(searchForm.getItemNumber())){
            predicate = ExpressionUtils.and(predicate,QItem.id.like(searchForm.getItemNumber()+"%"));
        }
        //商品名称
        //多个关键词用and查询
        if (StringUtils.isNotBlank(searchForm.getItemName())){
            String[] all = searchForm.getItemName().trim().split(" ");
            for (String s : all) {
                if (!StringUtils.isBlank(s)) {
                    predicate = ExpressionUtils.and(predicate,QItem.itemName.like("%"+s+"%")) ;
                    predicate = ExpressionUtils.and(predicate,QItem.itemName.like("%"+s+"%"));
                }
            }
        }
        //分类
        if (StringUtils.isNotEmpty(searchForm.getClassName())&&StringUtils.isNotBlank(searchForm.getClassName())){
            predicate = ExpressionUtils.and(predicate,QType.className.eq(searchForm.getClassName()));
        }

        //返回的map
        List<ItemDTO> dtos = queryFactory.select(Projections.bean(ItemDTO.class,QItem.id,QItem.itemName,QItem.price,QType.className,QItem.brand,QItem.isSaled,QItem.createTime))
                                .distinct()
                                .from(QItemType)
                                .leftJoin(QItem).on(QItemType.itemId.eq(QItem.id))
                                .leftJoin(QType).on(QItemType.typeId.eq(QType.id))
                                .where(predicate)
                                .orderBy(QItem.createTime.desc())
                                .offset(pageNumber)
                                .limit(pageSize)
                                .fetch();
        Long count = queryFactory.select(Projections.bean(ItemDTO.class,QItem.id,QItem.itemName,QItem.price,QType.className,QItem.brand,QItem.isSaled,QItem.createTime))
                                    .distinct()
                                    .from(QItemType)
                                    .leftJoin(QItem).on(QItemType.itemId.eq(QItem.id))
                                    .leftJoin(QType).on(QItemType.typeId.eq(QType.id))
                                    .where(predicate)
                                    .orderBy(QItem.createTime.desc())
                                    .fetchCount();
        Map map = returnMap(dtos,count);
        return map;

    }

    private Map returnMap(Object dtos, Long count) {


        Map map = new HashMap();

        map.put("dtos",dtos);
        map.put("count",count);
        return map;
    }


}
